package com.zhuhjay.config.plugins;

import com.google.common.collect.Sets;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.Operation;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.Collections;
import java.util.List;

import static springfox.documentation.service.ParameterType.BODY;

/**
 * @author ZhuHJay
 * @date 2023/3/21 15:46
 */
@Component
public class LoginApiScannerPlugin implements ApiListingScannerPlugin {

    /** 实现此方法可手动添加ApiDescriptions */
    @Override
    public List<ApiDescription> apply(DocumentationContext context) {
        RequestParameter parameter = new RequestParameterBuilder()
                .in(BODY)
                .name("userLogin")
                .description("userLogin")
                .required(true)
                // Model数据修改不进去, 需要的东西太多了, 暂时不管了
                .query(q -> q.model(
                        model -> model.referenceModel(
                                ref -> ref.key(
                                        refModel -> refModel.qualifiedModelName(
                                                qualifiedModelName -> qualifiedModelName.namespace("com.zhuhjay.model.dto").name("UserLoginDto")
                                        )
                                )
                        ).name("UserLoginDto")
                ))
                .build();

        Operation usernamePasswordOperation = new OperationBuilder(new CachingOperationNameGenerator())
                .method(HttpMethod.valueOf("POST"))
                .summary("用户登录")
                .notes("用户登陆获取token")
                .tags(Collections.singleton("系统用户控制器"))
                .uniqueId("login")
                .requestParameters(Collections.singletonList(parameter))
                .consumes(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .produces(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .build();

        ApiDescription loginApiDescription = new ApiDescription(
                "login", "/user/login",
                "登录接口", "用户登录",
                Collections.singletonList(usernamePasswordOperation), false);

        return Collections.singletonList(loginApiDescription);
    }

    /**
     * 是否使用此插件
     * @param documentationType swagger文档类型
     * @return true 启用
     */
    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
